﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using DataAccess;
using Common;
using Entities;

namespace Business
{
    public class ProgrammeBusiness
    {
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static ProgrammeBusiness Instance { get; private set; }
        /// <summary>
        /// Initializes the <see cref="ProgrammeBusiness" /> class.
        /// </summary>
        static ProgrammeBusiness() { Instance = new ProgrammeBusiness(); }

        /// <summary>
        /// Shows the list programme.
        /// </summary>
        /// <param name="active">if set to <c>true</c> [active].</param>
        /// <returns></returns>
        public Collection<Programme> ShowListProgramme(bool active)
        {
            try
            {
                return ProgrammeDataAccess.Instance.ShowListProgramme(active);
            }
            catch (SqlException)
            {
                return null;
            }
        }
        /// <summary>
        /// Uniques the name of the programme.
        /// </summary>
        /// <param name="programmeName">Name of the programme.</param>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public bool UniqueProgrammeName(string programmeName, Guid id)
        {
            try
            {
                var programme = ProgrammeDataAccess.Instance.ShowListProgramme(true);
                var result = programme.SingleOrDefault(c => c.ProgrammeName == programmeName && c.ProgrammeId != id);
                return result == null;
            }
            catch (SqlException)
            {
                return false;
            }
        }
        /// <summary>
        /// Inserts the specified programme.
        /// </summary>
        /// <param name="programme">The programme.</param>
        /// <returns></returns>
        public bool Insert(Programme programme)
        {
            if (programme == null) return false;
            if (string.IsNullOrEmpty(programme.ProgrammeName))
                return false;
            if (programme.ContactId == Guid.Empty)
                return false;


            var result = ProgrammeDataAccess.Instance.Insert(programme);
            return result == 1;
        }

        /// <summary>
        /// Updates the specified programme.
        /// </summary>
        /// <param name="programme">The programme.</param>
        /// <returns></returns>
        public bool Update(Programme programme)
        {
            if (programme == null) return false;
            if (string.IsNullOrEmpty(programme.ProgrammeName))
                return false;
            if (programme.ContactId == Guid.Empty)
                return false;
            var programmeDataAccess = new ProgrammeDataAccess();
            var result = programmeDataAccess.Update(programme);
            return result == 1;
        }
        /// <summary>
        /// Gets the programme by id.
        /// </summary>
        /// <param name="programmeId">The programme id.</param>
        /// <returns></returns>
        public Programme GetProgrammeById(string programmeId)
        {
            return ProgrammeDataAccess.Instance.GetProgrammeById(programmeId);
        }


        /// <summary>
        /// Deletes the specified p.
        /// </summary>
        /// <param name="p">The p.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool Delete(string p)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Filters the specified keyword.
        /// </summary>
        /// <param name="keyword">The keyword.</param>
        /// <param name="includeInactive">if set to <c>true</c> [include inactive].</param>
        /// <returns></returns>
        public Collection<Programme> Filter(string keyword, bool includeInactive)
        {
            return ProgrammeDataAccess.Instance.Filter(keyword, includeInactive);
        }

        /// <summary>
        /// Pagings the specified page number.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="listProgramme">The list programme.</param>
        /// <returns></returns>
        public Collection<Programme> Paging(int pageNumber, int pageSize, Collection<Programme> listProgramme)
        {
            int totalPage = listProgramme.Count % pageSize == 0 ? listProgramme.Count / pageSize : listProgramme.Count / pageSize + 1;
            if (pageNumber <= totalPage)
            {
                return new Collection<Programme>(listProgramme.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList());
            }
            return null;
        }
        /// <summary>
        /// Checks the linkto service.
        /// </summary>
        /// <param name="contactId">The contact id.</param>
        /// <returns></returns>
        public bool CheckLinktoService(string contactId)
        {
            try
            {
                return ProgrammeDataAccess.Instance.CheckLinkToService(contactId);
            }
            catch (SqlException)
            {
                return false;
            }
        }
    }
}
